package com.edu.zju.scs.panacea.entity;

import com.edu.zju.scs.panacea.common.Global;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by liangyue on 2017/10/9.
 */
@SuppressWarnings("unchecked")
public class BaseResult<T>  implements Serializable{

    private int code = Global.Code.success;

    private String message = Global.Message.success;

    @JsonSerialize(include= JsonSerialize.Inclusion.NON_EMPTY)
    private T object = (T) new Object();

    @JsonSerialize(include= JsonSerialize.Inclusion.NON_EMPTY)
    private Map<String, Object> map = new HashMap<>();

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getObject() {
        return object;
    }

    public void setObject(T object) {
        this.object = object;
    }

    public Map<String, Object> getMap() {
        return map;
    }

    public void setMap(Map<String, Object> map) {
        this.map = map;
    }

    public BaseResult(String errorMsg) {
        this.message = errorMsg;
        this.code = Global.Code.error;
        this.object = (T) new Object();
        this.map = new HashMap<>();
    }

    public BaseResult() {
        this.object = (T) new Object();
        this.map = new HashMap<>();
    }
    public void success() {
        this.code = Global.Code.success;
    }

    public void success(T object) {
        this.code = Global.Code.success;
        this.message = Global.Message.success;
        this.object = object;
        this.map = new HashMap<>();
    }

    public void error() {
        this.code = Global.Code.error;
        this.message = Global.Message.error;
        this.object = (T) new Object();
        this.map = new HashMap<>();
    }

    public void error(String message) {
        this.code = Global.Code.error;
        this.message = message;
    }

    public void error(Global.ErrorEncode error) {
        this.code = error.getErrorNo();
        this.message = error.getName();
        this.object = (T) new Object();
        this.map = new HashMap<>();
    }

    @Override
    public String toString() {
        return "BaseResult{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", object=" + object +
                ", map=" + map +
                '}';
    }
}